//配置信息
var config = {
	server:'http://iess.03199.com/api/',
	client:_project_name,
	alertTime:1500
}

config.server='http://localhost:8081/';
//config.server='http://192.168.31.134:8081/';
toastr.options = {
	"closeButton" : true,
	"debug" : false,
	"positionClass" : "toast-top-center",
	"onclick" : null,
	"showDuration" : "1000",
	"hideDuration" : "1000",
	"timeOut" : "5000",
	"extendedTimeOut" : "1000",
	"showEasing" : "swing",
	"hideEasing" : "linear",
	"showMethod" : "fadeIn",
	"hideMethod" : "fadeOut"
}

var session = {
	setToken : function(token) {
		sessionStorage['token'] = token;
	},
	getToken : function() {
		return sessionStorage['token'];
	},
	getUser:function(){
		var u = sessionStorage['_user_info'];
		try {
			u = JSON.parse(u);
		} catch (e) {
			u = {};
		}
		return u;
	},
	setUser:function(u){
		sessionStorage['_user_info'] = JSON.stringify(u);
	},
	getMenu:function(){
		var u = sessionStorage['_menu_array'];
		try {
			u = JSON.parse(u);
		} catch (e) {
			u = {};
		}
		return u;
	},
	setMenu:function(u){
		sessionStorage['_menu_array'] = JSON.stringify(u);
	},
	setSchID : function(schID) {
		sessionStorage['_sch_id'] = schID;
	},
	getSchID : function() {
		return sessionStorage['_sch_id'];
	}
}

// 将url参数格式转成json格式
$.toJson = function(arg){
	if(!arg)return {};
    var ret = {};//
    arg.replace(/(\w+)=(\w+)/ig, function(a, b, c){ret[b] = unescape(c);});
    return ret;
}

//获取url参数
$.url = function(arg) {
	var param = window.location.search;
	if(arg){
	    var reg = new RegExp("(^|&)" + arg + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
	    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
	    if (r != null) return decodeURI(r[2]); return null; //返回参数值
	}else{
		return $.toJson(param);
	}
}

//设置默认超时30秒
$.ajaxSettings.timeout=300000;//设置默认超时时间

//封装post方法
$.post = function (options) {
   
	//自动封装url
    options.url = config.server + options.url;
   
    if (!options.dataType) options.dataType = 'json';
	
	//	options.contentType = 'application/json;charset=UTF-8';
	
	//在成功回调之前配合后台接口做些事情
	var fo = options.success;
	options.success = function(a,b,c){
		if(a.header && a.header.status === '401'){
			location.href = config.client;
		}
		if(fo) fo(a,b,c);
	};
	
	options.data = options.data||{};
	options.data.token = session.getToken();
	if(!options.data.iess) options.data.iess = session.getUser()['userName'];

	if (options.contentType == 'application/json;charset=UTF-8') {
	    if (session.getToken()) {
	        options.headers = { Authorization: session.getToken() };
	    }
	    options.data = JSON.stringify(options.data);
	} else {
	    if (session.getToken()) {
	        options.data.token = session.getToken();
	    }
	}

	//
	options.wrap = true;
    options.type = 'POST';
    return $.ajax(options);
  };

//封装get方法
$.get = function(options){
	//自动封装url
	options.url = config.client + options.url;
	options.dataType = 'text';
	
	//在成功回调之前配合后台接口做些事情
	var fo = options.success;
	options.success = function(a,b,c){
		if(a.header && a.header.status === '401'){
			//alert(a.header.message);
		}
		if(fo) fo(a,b,c);
	};
	
	//
	options.wrap = true;
	options.type = 'GET';
	return $.ajax(options);
};

//封装get方法
$.getData = function(options){
	//自动封装url
	options.url = config.server + options.url;
	options.dataType = 'json';
	
	//在成功回调之前配合后台接口做些事情
	var fo = options.success;
	options.success = function(a,b,c){
		if(a.header && a.header.status === '401'){
			//alert(a.header.message);
		}
		if(fo) fo(a,b,c);
	};
	
	//
	options.wrap = true;
	options.type = 'GET';
	return $.ajax(options);
};

//封装PUT方法
$.put = function(options){
	//自动封装url
	options.url = config.server + options.url;
	options.dataType = 'json';
	
	//在成功回调之前配合后台接口做些事情
	var fo = options.success;
	options.success = function(a,b,c){
		if(a.header && a.header.status === '401'){
			//alert(a.header.message);
		}
		if(fo) fo(a,b,c);
	};
	
	//
	options.wrap = true;
	options.type = 'PUT';
	return $.ajax(options);
};

//封装DELETE方法
$.delete = function(options){
	//自动封装url
	options.url = config.server + options.url;
	options.dataType = 'json';
	
	//在成功回调之前配合后台接口做些事情
	var fo = options.success;
	options.success = function(a,b,c){
		if(a.header && a.header.status === '401'){
			//alert(a.header.message);
		}
		if(fo) fo(a,b,c);
	};
	
	//
	options.wrap = true;
	options.type = 'DELETE';
	return $.ajax(options);
};